
<template>
    <el-input ref="inputCard"  v-model="currentValue" placeholder="" :disabled="disableds2"  :readonly="disableds2"  :index="index"  :fontSize="fontSize" :required="isRequired" :id="id" class='myInput' :style="styles" clearable size="small"  @blur="check"   suffix-icon="el-icon-edit">
       
    
    </el-input>
</template>
 
<script>

    export default {
        props:['id','value','styles','disableds','data','isreadonly','index','isRequired','isPassword','extype','exkey','expost','appid','dataField','fontSize'],
        name: "input-card",
        data(){
            return {
                currentValue:this.value,
                currentValueOld:"",
                first:"Y",
                disableds2:this.disableds,
                p:'',
                isValidate:false,
                isreadonlys:this.isreadonly,
                cId:""
            }
        },
        created(){
            
            this.setData();
            try{
              
              if(this.exkey.readonlys){
                  for(let i=0;i<this.exkey.readonlys.length;i++){
                      let id=this.exkey.readonlys[i].id;
                      let opr=this.exkey.readonlys[i].opr;
                      let vals=this.exkey.readonlys[i].val;
                      let val_=this.data[this.p][id];
                      if(opr=="等于"&&vals==val_){
                          this.isreadonlys="Y";
                      }
                      if(opr=="不等于"&&vals!=val_){
                          this.isreadonlys="Y";
                      }

                  }
              }
          }catch(e){}
          if(this.isreadonlys=="Y")
              this.disableds2=true;
          
          
            
        },
        mounted(){
            
            try{
                this.$refs.inputCard.$parent.$parent.$children[0].$el.style.fontSize=this.fontSize
            }catch(e){}
        },
        methods:{
            setData(){
               
                if(!this.value){
                    if(this.data){
                        let ids=this.id.split(".");
                        if(ids.length>1){
                            let p=ids[0];
                            let id_=ids[1];
                            this.p=p;
                            this.cId=id_;
                            if(this.index||this.index=='0'){
                                try{
                                    this.currentValue=unescape(this.data[p][this.index][id_]);
                                }catch(e){}
                            }else{
                                try{
                                    this.currentValue=unescape(this.data[p][id_]);
                                }catch(e){}
                            }
                            
                        }else{
                            //alert(this.index)
                            if(this.dataField){
                                this.p=this.dataField;
                                this.cId=this.id;
                                if(this.index||this.index=='0'){
                                    try{
                                        this.currentValue=unescape(this.data[this.dataField][this.index][this.id]);
                                    }catch(e){}
                                }else{
                                    try{
                                        this.currentValue=unescape(this.data[this.dataField][this.id]);
                                    }catch(e){}
                                }
                            }else if(this.data[this.id])
                                this.currentValue= unescape(this.data[this.id]);
                        }
                    
                    }
                }else{
                    
                    let data={};
                    data.id=this.id;
                    data.val=this.value;
                    data.index=this.index;
                    this.$emit('input',JSON.stringify(data));
                }
            },
            check(){
                if(this.isValidate){
                     let msg=this.isCardID(this.currentValue);
                     if(msg){
                        this.$message.error(msg);
                        this.currentValue="";
                         if(this.index||this.index=='0'){
                            try{
                                this.$set(this.data[this.p][this.index],this.cId,"");
                            }catch(e){}
                        }else{
                            try{
                            this.$set(this.data[this.p],this.cId,"");
                            }catch(e){}
                        }
                     }
                }
            },
            isCardID(sId){
                var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
                
                var iSum=0 ;
                var info="" ;
                if(!/^\d{17}(\d|x)$/i.test(sId)) return "你输入的身份证长度或格式错误,请检查！";
                sId=sId.replace(/x$/i,"a");
                if(aCity[parseInt(sId.substr(0,2))]==null) return "你的身份证地区非法,请检查！";
                var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
                var d=new Date(sBirthday.replace(/-/g,"/")) ;
                if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "身份证上的出生日期非法,请检查！";
                for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ;
                if(iSum%11!=1) return "你输入的身份证号非法,请检查！";
                //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
                return "";
            },
            getSex(card){
                let sex="";
                //获取性别 
                if(parseInt(card.substr(16,1))%2==1){ 
                   sex="男";
                }else 
                { 
                  sex="女";
                } 
                return sex;
            },
            getAge(identityCard){
                var len = (identityCard + "").length;
                if (len == 0) {
                    return 0;
                } else {
                    if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
                    {
                        return 0;
                    }
                }
                var strBirthday = "";
                if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
                {
                    strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
                }
                if (len == 15) {
                    strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
                }
                //时间字符串里，必须是“/”
                var birthDate = new Date(strBirthday);
                var nowDateTime = new Date();
                var age = nowDateTime.getFullYear() - birthDate.getFullYear();
                //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
                if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
                    age--;
                }
                return age;
            },
            getBirth(identityCard){
                var len = (identityCard + "").length;
                if (len == 0) {
                    return 0;
                } else {
                    if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
                    {
                        return 0;
                    }
                }
                var strBirthday = "";
                if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
                {
                    strBirthday = identityCard.substr(6, 4) + "-" + identityCard.substr(10, 2) + "-" + identityCard.substr(12, 2);
                }
                if (len == 15) {
                    strBirthday = "19" + identityCard.substr(6, 2) + "-" + identityCard.substr(8, 2) + "-" + identityCard.substr(10, 2);
                }
                return strBirthday;
            }
        },
        watch:{
            currentValue: {
                handler:function (val,oldval) { //currentValue值变动就向父组件传值

                  
                     /*if(this.first=='Y'){
                        this.currentValueOld=this.currentValue;
                        this.first='N';
                    }*/
                    if(this.currentValueOld==this.currentValue){
                        return;
                    }
                    this.GLOBAL.isChange=true;
                    if(!val){
                        val="";
                    }
                     if(this.index||this.index=='0'){
                        try{
                            this.$set(this.data[this.p][this.index],this.cId,val);
                        }catch(e){}
                    }else{
                        try{
                           this.$set(this.data[this.p],this.cId,val);
                        }catch(e){}
                    }
                    

                    /*let data={};
                    data.id=this.id;
                    data.val=val;
                    data.index=this.index;

                    this.$emit('input',JSON.stringify(data));
                    this.$emit('on-change',JSON.stringify(data));*/

                    if(this.expost){
                        let exposts=this.expost.split("#");
                        let url=exposts[0];
                        let params={};
                        params.card=val;
                        if(exposts.length>1){
                            let pp=exposts[1].split(".");
                            let p=pp[0];
                            let id=pp[1];
                            params.exKey=this.data[p][id];
                        }


                        this.$post(this.appid,url,params).then(datas=>{
                            for(let key in datas){
                                if(key=="msg"){
                                    this.$message.error(datas[key]);
                                }else{
                                    let keys=key.split(".");
                                    if(keys.length>1){
                                        this.$set(this.data[keys[0]],keys[1],datas[key])
                                    }else{
                                        this.$set(this.data,key,datas[key]);
                                    }
                                    /*
                                    let data={};
                                    data.id=key;
                                    data.val=datas[key];
                                    data.index=this.index;
                                    
                                    this.$emit('input',JSON.stringify(data));
                                    this.$emit('on-change',JSON.stringify(data));*/
                                }
                            
                            }
                        })
                    }
                    this.isValidate=true;
                    if(this.exkey){
                       
                        try{
                            this.isValidate=false;
                            let exkeys=this.exkey.split(".");
                            let p=exkeys.length>1 ? exkeys[0]:this.p;
                            let id_=exkeys.length>1 ? exkeys[1]:exkeys[0];
                            if(unescape(this.data[p][id_])=='内地居民身份证')
                                this.isValidate=true;
                            else
                                this.isValidate=false;
                        }catch(e){
                            try{
                                this.isValidate=false;
                                let exkeys=this.exkey.val.split(".");
                                let p=exkeys.length>1 ? exkeys[0]:this.p;
                                let id_=exkeys.length>1 ? exkeys[1]:exkeys[0];
                                if(unescape(this.data[p][id_])=='内地居民身份证')
                                    this.isValidate=true;
                                else
                                    this.isValidate=false;
                            }catch(e){}
                           
                        }
                        
                    }
                    if(val.length>=15&&this.isValidate){
                        let msg=this.isCardID(val);
                        if(msg==""){
                            let extypes=this.extype.split("|");
                            let age=this.getAge(val);
                            let sex=this.getSex(val);
                            let birth=this.getBirth(val);
                            for(let i=0;i<extypes.length;i++){
                                let types=extypes[i].split(":");
                                console.log("types======="+types[0]+"=="+extypes[i])
                                 let p="";
                                if(types[0]=='age'){
                                    let data2={};
                                    let id_=types[1];
                                   
                                    if(id_.indexOf(".")==-1){
                                        //id_=this.p+"."+id_;
                                        p=this.p;
                                    }else{
                                        let ids_=id_.split(".");
                                        p=ids_[0];
                                        id_=ids_[1];
                                    }
                                    
                                    if(this.index||this.index=='0'){
                                        this.$set(this.data[p][this.index],id_,age)
                                    }else
                                        this.$set(this.data[p],id_,age)
                                    /*
                                    data2.id=id_;
                                    data2.val=age;
                                    data2.index=this.index;
                                    this.$emit('input',JSON.stringify(data2));
                                    this.$emit('on-change',JSON.stringify(data2));*/
                                }else if(types[0]=='sex'){
                                    
                                    let data2={};
                                    let id_=types[1];

                                    if(id_.indexOf(".")==-1){
                                        //id_=this.p+"."+id_;
                                        p=this.p;
                                    }else{
                                        let ids_=id_.split(".");
                                        p=ids_[0];
                                        id_=ids_[1];
                                    }
                                    console.log("p===="+p+"=="+this.index+"=="+id_+"="+sex)
                                     if(this.index||this.index=='0'){
                                        this.$set(this.data[p][this.index],id_,sex)
                                     }else
                                        this.$set(this.data[p],id_,sex)

                                }else if(types[0]=='birth'){
                                    let data2={};
                                    let id_=types[1];
                                   if(id_.indexOf(".")==-1){
                                        //id_=this.p+"."+id_;
                                        p=this.p;
                                    }else{
                                        let ids_=id_.split(".");
                                        p=ids_[0];
                                        id_=ids_[1];
                                    }
                                    if(this.index||this.index=='0'){
                                        this.$set(this.data[p][this.index],id_,birth)
                                    }else
                                         this.$set(this.data[p],id_,birth)
                                }
                            }
                        }else{
                            
                        }
                    }
                
                }
            },
            value:function (val) { //对值进行验证0
                
            },
            data:{
                handler: function (newVal) {
                    this.setData()
                },      
                deep: true    //深度监听
            }
        }
    }
</script>
<style scoped>
    .el-input__inner{
        height:25px;
    }
    input{
        height:25px;
    }
</style>